//----------------------------------------------------------------------------
// View 'Home'
//----------------------------------------------------------------------------
Ti.include('lib/include.js');
Ti.include('lib/constants.js');
Ti.include('lib/strings.js');
Ti.include('home_odk_ios.js');
Ti.include('home_nbs_ios.js');
Ti.include('home_info_ios.js');


var winHome = Titanium.UI.currentWindow;
winHome.addEventListener('focus', function(e) {
   log("Window 'Home' actief");
});


//----- 'Info' button
var btnInfo = Titanium.UI.createButton({
   title: 'Info'
});
btnInfo.addEventListener('click', function(e) {
   showView('INFO');
});

winHome.leftNavButton = btnInfo;



//----- 'Contact' button
var btnContact = Titanium.UI.createButton({
   title : 'Contact'
});
winHome.rightNavButton = btnContact;

if (isIpad()) {
   log("Contactopties voor ipad instellen");
   btnContact.addEventListener('click', function(e) {
      createEmail();
   });
} 
else if (isIphone()) {
   log("Contactopties voor iphone instellen");
   btnContact.addEventListener('click', function(e) {
      contactSelectionDialog.show();
   });
   var contactSelectionDialog = Titanium.UI.createOptionDialog({
      title : 'Direct contact opnemen',
      options : ["Bel 'Onder de Kerk'", "Bel 'Niels Bohrstraat'", 'Stuur Email', 'Annuleren'],
      cancel : 3
   });
   contactSelectionDialog.addEventListener('click', function(event) {
      if (event.index == 0) {
         //Per telefoon, praktijk Onder de Kerk
         Ti.Analytics.featureEvent(APP_EVENTS.EVENT_PHONE_DIALED);
         Titanium.Platform.openURL('tel:0433614444');
      } 
      else if (event.index == 1) {
         //Per telefoon, praktijk Niels Bohrstraat
         Ti.Analytics.featureEvent(APP_EVENTS.EVENT_PHONE_DIALED);
         Titanium.Platform.openURL('tel:0433612818');
      } 
      else if (event.index == 2) {
         //per email
         createEmail();
      } 
      else {
         Ti.API.debug('Annuleren gekozen');
      }
   });
}


var viewHome = Titanium.UI.createView({
   width : '100%',
   height : '100%'
});

var scrollViewHome = Ti.UI.createScrollView({
   contentWidth : 'auto',
   contentHeight : 'auto',
   showVerticalScrollIndicator : true,
   showHorizontalScrollIndicator : true
});


//----- iphone versie
if (isIphone()) {
   var lblHome1 = Titanium.UI.createLabel({
      textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
      font : {
         fontSize : 26,
         fontWeight : 'bold',
         fontFamily : customFont2
      },
      color : hh_lichtblauw,
      shadowColor : schaduwgroen,
      shadowOffset : {
         x : 4,
         y : 4
      },
      top : '40',
      text : T_HOME_TITLE
   });

   var lblHome2 = Titanium.UI.createLabel({
      top : '80',
      textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
      font : {
         fontSize : 14,
         fontWeight : 'bold',
         fontFamily : customFont
      },
      color : hh_lichtblauw,
      text : T_HOME_UITLEG
   });
   
   var btnLoc1 = Ti.UI.createButton({
      top : '130',
      width : '200',
      height : '50',
      font : {
         fontSize : 14,
         fontWeight : 'bold',
         fontFamily : customFont
      },
      color : wit,
      title : 'Praktijk Onder de Kerk',
      backgroundColor: hh_lichtblauw,
      backgroundImage: 'images/backgrounds/bg-lichtblauw.png',
      borderRadius : '5',
      borderColor : hh_lichtblauw
   });
   btnLoc1.addEventListener('click', function(e) {
      showView('1');
   });

   var btnLoc2 = Ti.UI.createButton({
      top : '200',
      width : '200',
      height : '50',
      font : {
         fontSize : 14,
         fontWeight : 'bold',
         fontFamily : customFont
      },
      color : wit,
      title : 'Praktijk Niels Bohrstraat',
      backgroundColor: hh_lichtblauw,
      backgroundImage: 'images/backgrounds/bg-lichtblauw.png',
      borderRadius : '5',
      borderColor : hh_lichtblauw
   });
   btnLoc2.addEventListener('click', function(event) {
      showView('2');
   });
   
   scrollViewHome.add(lblHome1, lblHome2, btnLoc1, btnLoc2);
   viewHome.add(scrollViewHome);
   winHome.add(viewHome);
   
   //Add some nice rearrangement of buttons on orientation change
   Ti.Gesture.addEventListener('orientationchange', function(e) {
      if (e.source.isLandscape()) {
         btnLoc1.top = '130';
         btnLoc1.left = '30';
         btnLoc2.top = '130';
         btnLoc2.right = '30';
      }
      else {
         btnLoc1.top = '130';
         btnLoc1.left = (Ti.Platform.displayCaps.platformWidth - btnLoc1.width) / 2;
         btnLoc2.top = '200';
         btnLoc2.right = (Ti.Platform.displayCaps.platformWidth - btnLoc2.width) / 2;
      }
   });
}
//----- ipad versie
else if (isIpad()) {
   var viewButtons = Titanium.UI.createView({
      top: '350',
      width: '500',
      height: '50'
   });
   
   var lblHome1 = Titanium.UI.createLabel({
      top : '150',
      text : T_HOME_TITLE,
      textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
      font : {
         fontSize : 50,
         fontWeight : 'bold',
         fontFamily : customFont2
      },
      color : hh_lichtblauw,
      shadowColor : schaduwgroen,
      shadowOffset : {
         x : 5,
         y : 5
      }
   });

   var lblHome2 = Titanium.UI.createLabel({
      top : '250',
      text : T_HOME_UITLEG,
      textAlign : Ti.UI.TEXT_ALIGNMENT_CENTER,
      font : {
         fontSize : 18,
         fontWeight : 'bold'
      },
      color : hh_lichtblauw
   });

   var btnLoc1 = Ti.UI.createButton({
      left: '0',
      width : '200',
      height : '50',
      title : 'Praktijk Onder de Kerk',
      font : {
         fontSize : 14,
         fontWeight : 'bold',
         fontFamily : customFont
      },
      color : wit,
      backgroundColor: hh_lichtblauw,
      backgroundImage: 'images/backgrounds/bg-lichtblauw.png',
      borderRadius : '5',
      borderColor : hh_lichtblauw
   });
   btnLoc1.addEventListener('click', function(event) {
      showView('1');
   });

   var btnLoc2 = Ti.UI.createButton({
      right: '0',
      width : '200',
      height : '50',
      title : 'Praktijk Niels Bohrstraat',
      font : {
         fontSize : 14,
         fontWeight : 'bold',
         fontFamily : customFont
      },
      color : wit,
      backgroundColor: hh_lichtblauw,
      backgroundImage: 'images/backgrounds/bg-lichtblauw.png',
      borderRadius : '5',
      borderColor : hh_lichtblauw
   });
   btnLoc2.addEventListener('click', function(event) {
      showView('2');
   });  

   viewButtons.add(btnLoc1, btnLoc2);
   scrollViewHome.add(lblHome1, lblHome2, viewButtons);
   viewHome.add(scrollViewHome);
   winHome.add(viewHome);

}



//----- Functions
function showView(view) {
   if (view == '1') {
      if (isIpad()) {
         winHome.setTitle("Praktijk 'Onder de Kerk'");   
      }
      else {
         winHome.setTitle("Onder de Kerk");
      }
      winHome.remove(viewHome);      
      winHome.add(viewLocatie1);      
      winHome.leftNavButton = navbtnBack1;
   }
   if (view == '2') {
      if (isIpad()) {
         winHome.setTitle("Praktijk 'Niels Bohrstraat'");   
      }
      else {
         winHome.setTitle("Niels Bohrstraat");
      }      
      winHome.remove(viewHome);      
      winHome.add(viewLocatie2);      
      winHome.leftNavButton = navbtnBack2;
   }
   if (view == 'INFO') {
      winHome.setTitle("Informatie");        
      winHome.remove(viewHome);      
      winHome.add(viewInfo);      
      winHome.leftNavButton = navbtnBackInfo;
      winHome.rightNavButton = btnVersie;
   }
}


function showHomeView(view) {
   if (view == '1') {
      winHome.remove(viewLocatie1);
   }
   if (view == '2') {
      winHome.remove(viewLocatie2);
   }
   if (view == 'INFO') {
      winHome.remove(viewInfo);
   }
   winHome.add(viewHome);
   winHome.leftNavButton = btnInfo;
   winHome.rightNavButton = btnContact;
   winHome.setTitle('Welkom');
}